package com.taiyi.renaissancemall.ui.main.brand


import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.View.GONE
import android.view.View.VISIBLE
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.taiyi.renaissancemall.R
import com.taiyi.renaissancemall.Utils.load
import com.taiyi.renaissancemall.Utils.loadRound
import com.taiyi.renaissancemall.adapter.AdapterManager
import com.taiyi.renaissancemall.adapter.baseadapter.MultiItemTypeAdapter
import com.taiyi.renaissancemall.base.BaseActivity
import com.taiyi.renaissancemall.bean.BrandListBean
import com.taiyi.renaissancemall.ui.main.goodsdetail.GoodsDetailActivity
import kotlinx.android.synthetic.main.activity_brand_list.*
import kotlinx.android.synthetic.main.base_top_white_background_layout.*

class BrandListActivity : BaseActivity<BrandListView, BrandListPresent>(), BrandListView,
    MultiItemTypeAdapter.OnItemClickListener, SpringView.OnFreshListener {


    override fun getLayoutId(): Int = R.layout.activity_brand_list

    var pageNo = 1
    var data = mutableListOf<BrandListBean.ComlistBean>()

    override fun initPresenter(): BrandListPresent = BrandListPresent()

    override fun initViews(savedInstanceState: Bundle?) {
//        titleTv.text = intent.getStringExtra("brandName")
        backIv.setOnClickListener { finish() }
        springView.header = DefaultHeader(this)
        springView.footer = DefaultFooter(this)
        springView.setGive(SpringView.Give.BOTH)
        springView.type = SpringView.Type.FOLLOW
        springView.setListener(this)
        var brandListAdapter = AdapterManager.getBrandListAdapter(this@BrandListActivity, data)
        recyclerView.run {
            layoutManager = LinearLayoutManager(this@BrandListActivity)
            adapter = brandListAdapter
        }
        brandListAdapter.setOnItemClickListener(this)
    }

    override fun initImmersionBar() {
        super.initImmersionBar()
        mImmersionBar.titleBar(top).init()
    }

    override fun initData() {
        showProgress()
        presenter.getBrandList()
    }

    override fun initEvent() {

    }

    override fun getBrandId(): String = intent.getStringExtra("brandId")
    override fun getFailure(msg: String?) {
        hideProgress()
        springView.onFinishFreshAndLoad()
        toast(msg)

    }

    override fun getPageNum(): String = pageNo.toString()
    override fun getSuccess(brandListBean: BrandListBean?) {
        hideProgress()

        springView.onFinishFreshAndLoad()
        titleTv.text = brandListBean?.brandname
        brandIv.loadRound(brandListBean?.xunchimg!!)
        brandLogoIv.load(brandListBean?.logo!!)
        var comlist = brandListBean?.comlist
        if (pageNo == 1) {
            data.clear()
            if (comlist.isNotEmpty())
                noDataIv.visibility = GONE
            else noDataIv.visibility = VISIBLE
        }
        data.addAll(comlist!!)
        recyclerView.adapter?.notifyDataSetChanged()

    }

    override fun onItemLongClick(
        view: View?,
        holder: RecyclerView.ViewHolder?,
        position: Int
    ): Boolean {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onItemClick(view: View?, holder: RecyclerView.ViewHolder?, position: Int) {
        var intent = Intent(this, GoodsDetailActivity::class.java)
        var bundle = Bundle()
        bundle.putString("commodityId", data[position].commodityid)
        intent.putExtras(bundle)
        startActivity(intent)


    }

    override fun onLoadmore() {
        pageNo += 1
        showProgress()
        presenter.getBrandList()
    }

    override fun onRefresh() {
        pageNo = 1
        showProgress()
        presenter.getBrandList()
    }
}
